{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e306140-a2ec-4263-bbb3-a183cca83c99",
   "metadata": {},
   "source": [
    "# 元组\n",
    "## 定义元组\n",
    "- 元组的特点：定义元组使用小括号，且逗号隔开各个数据，数据可以是不同的数据类型\n",
    "- 【一个元组可以存储多个数据，但是元组内的数据是不能修改的】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c3fb29a-a8da-4da2-a944-7f89314269b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "t1 = (10, 20, 30)   # tuple元组 定有多个元组\n",
    "t2 = (10, )    # 在定义一个元组的时候，后面必须要都逗号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "134431e3-6830-460f-a305-9ea3b36c30e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "(10, 20, 30)\n",
      "<class 'tuple'>\n",
      "<class 'int'>\n",
      "<class 'str'>\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "t1 = (10, 20, 30)    # 定义多个元组\n",
    "print(type(t1))    #<class 'tuple'> 元组类型\n",
    "print(t1)    # (10, 20, 30)\n",
    "\n",
    "t2 = (10, )    #定义一个元组\n",
    "print(type(t2))    #<class 'tuple'> 元组类型\n",
    "\n",
    "t2_test = (10)\n",
    "print(type(t2_test))    #<class 'int'> 整数类型\n",
    "\n",
    "t3 = ('hello')\n",
    "print(type(t3))    #<class 'str'> 字符串类型\n",
    "\n",
    "t4 = (\"world\", )\n",
    "print(type(t4))    #<class 'tuple'> 元组类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19ef69af-ca9b-48db-8ba8-7ee795108f51",
   "metadata": {},
   "source": [
    "# 元组的常见操作类型\n",
    "## 按下标查找数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8f0dfd7b-fc2c-45b9-8798-18f506f093fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bb\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "tuple1 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(tuple1[1])    # bb 注意这里要用[] 方括号，跟列表一样"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e79632a-472b-4b4d-9330-b153c3a41201",
   "metadata": {},
   "source": [
    "index() 查找某个数据，如果数据存在则返回对应的下标，否则报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f7a08fdc-1be2-4995-b938-ea4f75d14396",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "tuple2 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(tuple2.index('bb'))    # 如果有相同的元素，它会查找第一个，跟列表一致"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71ac6357-b43c-45a9-b816-546b198f1ed5",
   "metadata": {},
   "source": [
    "count()统计某个数据在元组中出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "32d9a355-1a67-45ad-a472-d132c193817d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "tuple3 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(tuple3.count('bb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb211357-5d75-4888-9541-7c8ad76e984c",
   "metadata": {},
   "source": [
    "len()统计元组中数据的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "19d3c3cb-0feb-4127-88c2-3fbfec7dc094",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "tuple4 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(len(tuple4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
